package com.wf.course.web.controller.ad;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.utils.page.PageBean;
import com.wf.course.model.ad.VipCard;
import com.wf.course.service.IVipCardService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 年卡Controller
 */
@RequestMapping("vipCard")
@Controller
public class VipCardController {

	@Autowired
	private IVipCardService vipCardService;
	

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param cardName
	 * @param cardStatus
	 * @param model
	 * @return
	 */
	@LogInfo("查询年卡包信息列表")
	@RequestMapping("vipCardList")
	public String adList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "cardName", required = false) String cardName, 
			@RequestParam(value = "cardStatus", required = false) String cardStatus, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("cardName", cardName);
		if(cardStatus==""){
			cardStatus=null;
		cons.put("cardStatus", cardStatus);
		}else{
			cons.put("cardStatus", cardStatus);	
		}
		PageBean<VipCard> pageBean = new PageBean<VipCard>(pageNo, pageSize, null, cons);
		pageBean.setUrl("vipCard/vipCardList");
		this.vipCardService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "ad/vipCardList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增或修改年卡包信息，页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			VipCard vipCard = vipCardService.queryVipCardById(id);
			model.addAttribute("vipCard", vipCard);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		  
		
		return "ad/vipCardEdit";
	}

	/**
	 * 新增和修改
	 * 
	 * @param vipCard
	 * @param imgFile
	 * @param request
	 * @return
	 */
	@LogInfo("新增或修改年卡包信息")
	@RequestMapping(value = "vipCardEdit" , produces = "text/html;charset=UTF-8")
	public String indexTypeEdit(@ModelAttribute VipCard vipCard, MultipartFile imgFile, HttpServletRequest request) {
		
		vipCardService.saveOrUpdateVipCard(vipCard,imgFile);
		
		return "redirect:/vipCard/vipCardList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除年卡包信息")
	@RequestMapping(value = "remove/{id}")
	public String removeAd(@PathVariable("id") Integer id) {
		
		vipCardService.deleteById(id);
		
		return "redirect:/vipCard/vipCardList";
	}
	

}
